/**
 * listing
 */

/**
 * 表单自定义控制器
 */
app.controller('btnCtrl', ['$scope', '$http', BtnController]);
function BtnController($scope, $http) {
	$scope.bindBtn = function(event) {
		switch(event){
			case 'event-to-add': // 新增listing子件
			{
				$scope.subGrid.sonlist.push({
					id: null,
					pcode: $scope.valueMap['code'],
					code: '保存后自动生成',
					name: '',
					subCodeMerge: '',
					num: '',
					bprice: '',
					linkIn: '',
					linkInMark: '',
					remark: '',
					invoiceModelNo: '',
					invoiceName: '',
					customsCode: '',
					countryName: '',
					exemptionTax: '',
					customsExplain: '',
					singleWeight: '',
				});
				break;
			}
			case 'event-to-del': // 删除选中数据
			{

				var datas = [];
				for (var i = 0; i < $scope.subGrid.sonlist.length; i++) {
					var data = $scope.subGrid.sonlist[i];
					if(data['ys_form_sonlist_checkbox'] == true) {
						datas.push(data);
					}
				}
				if (datas.length == 0) {
					layer.alert('尚未选中数据');
				} else {
					if (datas.length >= t.c.subGridObj.c.sonlist.length){
						return layer.msg("删除失败,子件列表至少需要一条!");
					}
					var _list = [];
					//console.log('datas',datas);
					for (var i = 0; i < datas.length; i++) {
						if(datas[i].id!=undefined && datas[i].id!=null && datas[i].id!=''){
							_list.push(datas[i].id);
						}
					}
					layer.confirm('确定要删除吗？', { icon: 3 }, function() {
						if(_list!=null && _list.length>0){
							layer.msg('处理中', { icon: 16, shade: 0.01 });
							var ids = '';
							for(var i=0;i<_list.length;i++){
								if(ids==''){
									ids = _list[i];
								}else {
									ids = ids + ',' +_list[i];
								}
							}
							$http.post('/api/inter/tpProductActive/toDel', {}, {params: {ids: ids,code:$scope.valueMap['code']}})
								.success(function (res) {
									if(res.code==0){
										layer.close();
										layer.msg(res.msg);
										var datasByNew = [];
										for (var i = 0; i < $scope.subGrid.sonlist.length; i++) {
											var data = $scope.subGrid.sonlist[i];
											if(data['ys_form_sonlist_checkbox'] == true) {
												console.log("data", data);
												continue;
											} else {
												datasByNew.push(data);
											}
										}
										$scope.subGrid.sonlist = datasByNew;
									}else{
										layer.close();
										layer.msg(res.msg);
									}


								})
								.error(function(result){
									layer.close();
									layer.msg("删除失败，请稍后再试！", { icon: 2 });
								});
						}else {
							var datasByNew = [];
							for (var i = 0; i < $scope.subGrid.sonlist.length; i++) {
								var data = $scope.subGrid.sonlist[i];
								if(data['ys_form_sonlist_checkbox'] == true) {
									console.log("data", data);
									continue;
								} else {
									datasByNew.push(data);
								}
							}
							$scope.subGrid.sonlist = datasByNew;
							layer.closeAll();
							layer.msg("删除成功！");
						}

					}, function() {
					});

				}

				break;
			}

			case 'event-to-ph': // 铺货
			{
				var loadIndex = layer.confirm('确定要铺货?', {icon: 3}, function () {
					//点击确定的回调
					layer.close(loadIndex);
					//var t = this;
					//var c = this.c;
					if ($scope.subGrid.sonlist.length < 0){
						return layer.msg("铺货失败,子件信息至少需要一条!");
					}
					var loadIndex = layer.load(2);
					$http.post('/api/inter/1688/get1688ProductDetail', {}, {params: {'code':$scope.valueMap['code']}})
						.success(function (result) {
							if(result.code==0){
								layer.close(loadIndex);
								layer.msg(result.msg, {icon: 1});
								//c.data = newData;
								//$('#' + c.id).empty();
								//return t.reload(c);
							}else {//失败
								layer.close(loadIndex);
								layer.msg(result.msg, {icon: 2});
							}
						})
						.error(function(result){
							layer.close(loadIndex);
							layer.msg("铺货失败，请稍后再试！", {icon: 2});
						});

				}, function () {});//点击取消的回调
				break;
			}
		};
	}

}



